package com.jy.records.service.impl;

import java.util.List;

import com.jy.users.domain.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.jy.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.jy.records.mapper.BorrowRecordsMapper;
import com.jy.records.domain.BorrowRecords;
import com.jy.records.service.IBorrowRecordsService;

/**
 * 借阅记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-29
 */
@Service
public class BorrowRecordsServiceImpl implements IBorrowRecordsService 
{
    @Autowired
    private BorrowRecordsMapper borrowRecordsMapper;

    /**
     * 查询借阅记录
     * 
     * @param recordId 借阅记录主键
     * @return 借阅记录
     */
    @Override
    public BorrowRecords selectBorrowRecordsByRecordId(Long recordId)
    {
        return borrowRecordsMapper.selectBorrowRecordsByRecordId(recordId);
    }

    /**
     * 查询借阅记录列表
     * 
     * @param borrowRecords 借阅记录
     * @return 借阅记录
     */
    @Override
    public List<BorrowRecords> selectBorrowRecordsList(BorrowRecords borrowRecords)
    {
        return borrowRecordsMapper.selectBorrowRecordsList(borrowRecords);
    }

    /**
     * 新增借阅记录
     * 
     * @param borrowRecords 借阅记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBorrowRecords(BorrowRecords borrowRecords)
    {
        int rows = borrowRecordsMapper.insertBorrowRecords(borrowRecords);
        insertUsers(borrowRecords);
        return rows;
    }

    /**
     * 修改借阅记录
     * 
     * @param borrowRecords 借阅记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBorrowRecords(BorrowRecords borrowRecords)
    {
        borrowRecordsMapper.deleteUsersByUserId(borrowRecords.getRecordId());
        insertUsers(borrowRecords);
        return borrowRecordsMapper.updateBorrowRecords(borrowRecords);
    }

    /**
     * 批量删除借阅记录
     * 
     * @param recordIds 需要删除的借阅记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBorrowRecordsByRecordIds(Long[] recordIds)
    {
        borrowRecordsMapper.deleteUsersByUserIds(recordIds);
        return borrowRecordsMapper.deleteBorrowRecordsByRecordIds(recordIds);
    }

    /**
     * 删除借阅记录信息
     * 
     * @param recordId 借阅记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBorrowRecordsByRecordId(Long recordId)
    {
        borrowRecordsMapper.deleteUsersByUserId(recordId);
        return borrowRecordsMapper.deleteBorrowRecordsByRecordId(recordId);
    }

    /**
     * 新增图书馆用户信息
     * 
     * @param borrowRecords 借阅记录对象
     */
    public void insertUsers(BorrowRecords borrowRecords)
    {
        List<Users> usersList = borrowRecords.getUsersList();
        Long recordId = borrowRecords.getRecordId();
        if (StringUtils.isNotNull(usersList))
        {
            List<Users> list = new ArrayList<Users>();
            for (Users users : usersList)
            {
                users.setUserId(recordId);
                list.add(users);
            }
            if (list.size() > 0)
            {
                borrowRecordsMapper.batchUsers(list);
            }
        }
    }
}
